package com.goktech.olala.client.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.goktech.olala.client.controller.basic.BasicController;
import com.goktech.olala.client.controller.basic.ResultStatus;
import com.goktech.olala.core.req.ReqBusiness;
import com.goktech.olala.core.resp.RespBusinessVo;
import com.goktech.olala.core.service.IBusinessService;
import com.goktech.olala.server.pojo.goods.Business;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author wusq
 * 商家控制器（店铺）
 */
@Controller
@RequestMapping(value = "/business")
public class BusinessController extends BasicController {
    @Autowired
    IBusinessService businessService;

    /*==============================================店铺信息==============================================*/

    /**
     * 查询店铺信息
     */
    @RequestMapping(value = "queryBusiness.do", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap queryMemberLoginLog(HttpServletRequest request, Integer sEcho,
                                        Integer iDisplayStart, Integer iDisplayLength) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = 10000;
        String respMsg = "查询成功!";
        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            ReqBusiness reqBusiness = buildReqBusiness(params);

            int pageIndex = iDisplayStart / iDisplayLength + 1;
            PageInfo<RespBusinessVo> pageInfo = businessService.queryBusiness(pageIndex, iDisplayLength, reqBusiness);
            if (pageInfo != null && CollectionUtils.isEmpty(pageInfo.getList())) {
                respCode = -10000;
                respMsg = "查询结果集为空!";
            }
            respData = super.respParamsFormat(respCode, respMsg, sEcho, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = -10001;
            respMsg = e.getMessage();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 添加店铺信息
     * @return
     */
    @RequestMapping(value = "insertBusiness.do")
    @ResponseBody
    public ModelMap saveBusiness(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = 10000;
        String respMsg = "保存成功!";
        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            ReqBusiness reqBusiness = buildReqBusiness(params);
            int result = businessService.saveBusiness(reqBusiness);
            respData = super.respParamsFormat(respCode, respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = -10001;
            respMsg = e.getMessage();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 根据Id查询店铺信息
     */
    @RequestMapping(value = "queryBusinessByKey.do")
    @ResponseBody
    public ModelMap queryBusinessByKey(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = 10000;
        String respMsg = "查询成功!";

        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            String businessId = params.getString("businessId");

            if (businessId == null) {
                respCode = -10002;
                respMsg = "输入参数错误！";

                respData = super.respParamsFormat(respCode, respMsg, null);
                resultMap.put("respData", respData);
                return resultMap;
            }
            RespBusinessVo respBusinessVo = businessService.queryBusinessById(businessId);
            if (respBusinessVo == null) {
                respCode = -10000;
                respMsg = "查询结果集为空!";
            }
            respData = super.respParamsFormat(respCode, respMsg, respBusinessVo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = -10001;
            respMsg = e.getMessage();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 修改店铺信息
     */
    @RequestMapping(value = "updateBusiness.do")
    @ResponseBody
    public ModelMap updateBusiness(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();

        Integer respCode = ResultStatus.SUCCESS.getCode();
        String respMsg = ResultStatus.SUCCESS.getMsg();
        JSONObject params = super.reqParamsFormat(request);
        ReqBusiness reqBusiness = buildReqBusiness(params);
        JSONObject respData = null;

        try {
            int result = businessService.saveBusiness(reqBusiness);
            respData = super.respParamsFormat(respCode, respMsg, result);
        } catch (Exception e) {
            respCode = ResultStatus.OPERATION_FAIL.getCode();
            respMsg = ResultStatus.OPERATION_FAIL.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);

        return resultMap;
    }

    /**
     * 删除店铺
     * @return
     */
    @RequestMapping(value = "removeBusiness.do")
    @ResponseBody
    public ModelMap removeOrder(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = ResultStatus.SUCCESS.getCode();
        String respMsg = ResultStatus.SUCCESS.getMsg();

        JSONObject respData = new JSONObject();

        try {
            JSONObject params = super.reqParamsFormat(request);
            String businessId = params.getString("businessId");
            if (businessId == null) {
                respCode = ResultStatus.OPERATION_FAIL.getCode();
                respMsg = ResultStatus.OPERATION_FAIL.getMsg();

                respData = super.respParamsFormat(respCode, respMsg, null);
                resultMap.put("respData", respData);
                return resultMap;
            }
            int result = businessService.removeBusinessById(businessId);
            respData = super.respParamsFormat(respCode, respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 构建查询实体类
     * @param params
     * @return
     */
    public ReqBusiness buildReqBusiness(JSONObject params) {
        return  JSONObject.toJavaObject(params,ReqBusiness.class);
        /*ReqBusiness reqBusiness = new ReqBusiness();
        String businessId = params.getString("businessId");
        String businessName = params.getString("businessName");
        String businessCode = params.getString("businessCode");
        String address = params.getString("address");
        String sendRange = params.getString("sendRange");
        Integer fansCount = params.getInteger("fansCount");
        Integer score = params.getInteger("score");
        Integer businessLevel = params.getInteger("businessLevel");
        String businessType = params.getString("businessType");
        Long terminalCode = params.getLong("terminalCode");
        Integer status = params.getInteger("status");

        reqBusiness.setBusinessId(businessId);
        reqBusiness.setBusinessName(businessName);
        reqBusiness.setBusinessCode(businessCode);
        reqBusiness.setAddress(address);
        reqBusiness.setSendRange(sendRange);
        reqBusiness.setFansCount(fansCount);
        reqBusiness.setScore(score);
        reqBusiness.setBusinessLevel(businessLevel);
        reqBusiness.setBusinessType(businessType);
        reqBusiness.setTerminalCode(terminalCode);
        reqBusiness.setStatus(status);

        return reqBusiness;*/
    }
}
